
#include <iostream>
#include <fstream>
#include "ftplib.h"
#include "ftpMgr.h"

static void write(std::string name, std::string &msg) {
    std::ofstream ofile;
    ofile.open(name, std::ios::binary);
    ofile << msg;
    ofile.close();
}


int case1() {
    // "ftp://192.168.2.114/scene/2019_05_30_19_19_28_78.jpg"
    std::string ftpUrl = "./scene/2019_05_30_19_19_28_78.jpg";
    std::string dstPath = "./2019_05_30_19_19_28_78.jpg";

    std::string user = "ftpuser";
    std::string passWord = "cdzs123456";

    ftplib ftpAgent = ftplib();

    if (ftpAgent.Connect("192.168.2.114") != 1) {
        printf("ftp connect error\n");
        return -1;
    }

    if (ftpAgent.Login("ftpuser", "cdzs123456") != 1) {
        printf("ftp login error\n");
        return -1;
    }
    int ret = -1;
    ret = ftpAgent.Get(dstPath.c_str(), ftpUrl.c_str(), ftplib::binary);
    std::cout << " down Status: " << ret << std::endl;

    std::vector<std::string> files;
    std::vector<std::string> dirs;
    ret = ftpAgent.Dir(files,dirs, "./");
    std::cout << " dir Status: " << ret << std::endl;
    std::cout << " dir : " << files.size()<< std::endl;

    // test Copy
    ftplib ftpAgent2 = ftpAgent;

    ret = ftpAgent.Put("./cameraDeployReq.protobin", "./scene/cameraDeployReq.protobin", ftplib::binary);
    std::cout << " up Status: " << ret << std::endl;

    ret = ftpAgent.Delete("./scene/cameraDeployReq.protobin");
    std::cout << " Del Status: " << ret << std::endl;

    int quitRet = ftpAgent.Quit();
    std::cout << " quit Status: " << ret << std::endl;

    std::string contentBin;
    ret = ftpAgent.Get2Mem(contentBin, ftpUrl.c_str(), ftplib::binary);
    std::cout << " Get Bin Status: " << ret << std::endl;

    write("contentBin.jpg", contentBin);
}

void case2() {
    ftpMgr *ftpAgentRoot = ftpMgr::instance();
//    ftpAgentRoot->init("192.168.2.114", "ftpuser", "cdzs123456");
    ftpAgentRoot->init("192.168.2.37", "ftpuser", "cdzs123456");

    std::string strData;
    int ret = -1;

    camera_t cam1;
    cam1.ip = "ip1";
    ret = ftpAgentRoot->Get2Mem(strData, "./0.txt", cam1);

    camera_t cam2;
    cam2.ip = "ip2";
    ret = ftpAgentRoot->Get2Mem(strData, "./1.txt", cam2);

    ftpAgentRoot->Clear("ip1");
    ret = ftpAgentRoot->Get2Mem(strData, "./1.txt", cam1);

    ftpAgentRoot->Clear("ip1");
    ftpAgentRoot->Clear("ip2");
    ret = ftpAgentRoot->Get2Mem(strData, "./1.txt", cam1);
    ret = ftpAgentRoot->Get2Mem(strData, "./1.txt", cam2);

    for (int i = 0; i < 100; i++) {
        camera_t cam ;
        cam.ip = std::to_string(i);
        ret = ftpAgentRoot->Get2Mem(strData, "./1.txt", cam);
        cout << "ret: " << ret << " for ip: " << i << std::endl;
    }
    std::cout << "END CASE 2 " << std::endl;
}

int main() {

    case1();
//    case2();
}